//
//  Player.cpp
//  petgame
//
//  Created by zz on 2018/12/13.
//

#include "Player.hpp"

Player*Player::create(PETTYPE type,PETSTATUS status,Vec2 pos){
    
    //创建角色对象
    auto player=new Player();
    //初始化角色
    if(player&&player->init(type,status,pos)){
        //自动释放
        player->autorelease();
        return player;
    }else{
        
        CC_SAFE_DELETE(player);
        return NULL;
    }
}

bool Player::init(PETTYPE type,PETSTATUS status,Vec2 pos){

    //初始化角色数据，类型状态，坐标
    this->_type=type;
    this->_status=status;
    this->setPosition(pos);
    
    //初始化角色类型
    this->initPet(type);

    return true;
}


void Player::initPet(PETTYPE type){
    
    switch (type) {
        case PETTYPE::CAT:{
            
        }
            break;
        case PETTYPE::DOG:{
            
            this->initWithFile("play1.png");
            this->setScale(1);
        }
            break;
        case PETTYPE::FISH:{
        
            
            
        }
            break;
        default:
            break;
    }
}

void Player::toPlay(){
//    创建角色玩耍的帧动画
    
//    停止所以动画
    this->stopAllActions();
    auto animation=Animation::create();
//    添加动画帧
    animation->addSpriteFrameWithFile("play2.png");
    animation->addSpriteFrameWithFile("play3.png");
    animation->addSpriteFrameWithFile("play4.png");
    animation->addSpriteFrameWithFile("play5.png");
//    设置循环
    animation->setLoops(4);
//    设置帧间隔
    animation->setDelayPerUnit(0.2);
//    动画播放完的回调
    auto call=CallFunc::create(CC_CALLBACK_0(Player::toNoraml, this));
    auto animate=Animate::create(animation);
    auto seq=Sequence::create(animate,call, NULL);
//    运行动画
    this->runAction(seq);
    
}
void Player::toSleep(){
    
    
    this->stopAllActions();
    auto animation=Animation::create();
    animation->addSpriteFrameWithFile("sleep1.png");
    animation->addSpriteFrameWithFile("sleep2.png");
    animation->addSpriteFrameWithFile("sleep3.png");
    animation->addSpriteFrameWithFile("sleep4.png");
    animation->addSpriteFrameWithFile("sleep5.png");
    animation->setLoops(-1);
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
    this->runAction(animate);
}


void Player::toEat(){
    
    
    
    this->stopAllActions();
    auto animation=Animation::create();
    animation->addSpriteFrameWithFile("eat1.png");
    animation->addSpriteFrameWithFile("eat2.png");
    animation->addSpriteFrameWithFile("eat3.png");
    animation->addSpriteFrameWithFile("eat4.png");
    animation->addSpriteFrameWithFile("eat5.png");
    animation->setLoops(2);
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
    auto callfun=CallFunc::create(CC_CALLBACK_0(Player::toNoraml,this));
    auto seq=Sequence::create(animate,callfun, NULL);
    this->runAction(seq);
}

void Player::toNoraml(){


    this->stopAllActions();
    auto animation=Animation::create();
    animation->addSpriteFrameWithFile("n1.png");
    animation->addSpriteFrameWithFile("n2.png");
    animation->addSpriteFrameWithFile("n3.png");
    animation->addSpriteFrameWithFile("n4.png");
    animation->addSpriteFrameWithFile("n5.png");
    animation->setLoops(-1);
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
    this->runAction(animate);
    
    
}


void  Player::hungerEffect(){
//    创建角色饥饿的表现动画
    
//    停止播放所以动画
    this->stopAllActions();
//    创建帧动画
    auto animation=Animation::create();
    
//    添加关键帧
    animation->addSpriteFrameWithFile("e1.png");
    animation->addSpriteFrameWithFile("e2.png");
    animation->addSpriteFrameWithFile("e3.png");
    animation->addSpriteFrameWithFile("e4.png");
    animation->addSpriteFrameWithFile("e5.png");
//    设置循环播放
    animation->setLoops(-1);
//      设置帧间隔
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
//    运行动画
    this->runAction(animate);
}

void Player::unhappyEffec(){
    
    
    this->stopAllActions();
    auto animation=Animation::create();
    animation->addSpriteFrameWithFile("unh1.png");
    animation->addSpriteFrameWithFile("unh2.png");
    animation->addSpriteFrameWithFile("unh3.png");
    animation->addSpriteFrameWithFile("unh4.png");
    animation->addSpriteFrameWithFile("unh5.png");
    animation->setLoops(-1);
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
    this->runAction(animate);
    
    
}


void Player::unsleepEffect(){
    
    
    
    this->stopAllActions();
    auto animation=Animation::create();
    animation->addSpriteFrameWithFile("unsl1.png");
    animation->addSpriteFrameWithFile("unsl2.png");
    animation->addSpriteFrameWithFile("unsl3.png");
    animation->addSpriteFrameWithFile("unsl4.png");
    animation->addSpriteFrameWithFile("unsl5.png");
    animation->setLoops(-1);
    animation->setDelayPerUnit(0.2);
    auto animate=Animate::create(animation);
    this->runAction(animate);
    
}
